﻿var number = 0;
var iID;
var drawCan="LXGT"; //对比典型功图名称
var currentID; //保存当前ID
//将两个一维数组合并成一个二维数组
function getArray1(d1, d2) {
    var len = d1.length;
    var array = new Array(len);
    for (var i = 0; i < len; i++) {
        array[i] = [d1[i], d2[i]];
    }
    return array;
}

//***************通用方法***************************//
//进度条js
jQuery.fn.anim_progressbar = function (aOptions) {
    // def values   
    var iCms = 200;
    var iMms = 30 * iCms;
    var iHms = 3600 * iCms;
    var iDms = 12 * 3600 * iCms;

    // def options
    var aDefOpts = {
        start: new Date(), // now
        finish: new Date().setTime(new Date().getTime() + 60 * iCms), // now + 60 sec
        interval: 100
    }

    var aOpts = jQuery.extend(aDefOpts, aOptions);
    var vPb = this;

    // each progress bar
    return this.each(
                 function () {

                     var iDuration = aOpts.finish - aOpts.start;
                     // calling original progressbar
                     $(vPb).children('.pbar').progressbar();

                     // looping process
                     var vInterval = setInterval(
                         function () {
                             var iLeftMs = aOpts.finish - new Date(); // left time in MS
                             var iElapsedMs = new Date() - aOpts.start, // elapsed time in MS
                                 iDays = parseInt(iLeftMs / iDms), // elapsed days
                                 iHours = parseInt((iLeftMs - (iDays * iDms)) / iHms), // elapsed hours
                                 iMin = parseInt((iLeftMs - (iDays * iDms) - (iHours * iHms)) / iMms), // elapsed minutes
                                 iSec = parseInt((iLeftMs - (iDays * iDms) - (iMin * iMms) - (iHours * iHms)) / iCms), // elapsed seconds
                                 iPerc = (iElapsedMs > 0) ? iElapsedMs / iDuration * 100 : 0; // percentages
                             $(vPb).children('.pbar').children('.ui-progressbar-value').css('width', iPerc + '%');
                             // in case of Finish
                         }, aOpts.interval
                     );
                 }
             ); //function ()
}

//*****************智能诊断部分*********************//
//初始化分数和建议
function initioalValueDesc() {
    
    if (number >8) {
        //得出最后一个评价项的分数
        var div_value = document.getElementById("9EVAL_VALUE");
        div_value.innerHTML = '<div id="9EVAL_VALUE">' + '<img src="images\\' + (number*2 + 59).toString() + '.png ' + '">' + '</div>';
        //得出总建议和总分
        var div_zongfenValue = document.getElementById("img_zongfen");
        div_zongfenValue.innerHTML = '<div id="img_zongfen">' + '<img src="images\\75-zong.png">' + '</div>';
        $("#txt_zdjy").val("油井供液不足比较严重，同时还伴有轻微的震动。");
        //清除进度条上正在检测项名称
        $("#div_jian9").hide();
        $("#div_zongfen").show();
        clearInterval(iID);
    }
    else {
        //得出[interNum]处的评价项得分
        var div_value = document.getElementById(number.toString() + "EVAL_VALUE");
        if (number == 3 || number == 4 || number == 6||number == 8) {
            div_value.innerHTML = '<div id=' + number.toString() + 'EVAL_VALUE' + '>' + '<img src="images\\yes.png ' + '">' + '</div>';
        }
        else {
            div_value.innerHTML = '<div id=' + number.toString() + 'EVAL_VALUE' + '>' + '<img src="images\\' + (number * 2 + 59).toString() + '.png ' + '">' + '</div>';
        }
        //让[interNum+1]处的评价项分数图片转动等待
        var div_value1 = document.getElementById((number+1).toString() + "EVAL_VALUE");
        div_value1.innerHTML = '<div id=' + (number+1).toString() + 'EVAL_VALUE' + '>' + '<img src="images\\100.gif">' + '</div>';
        //生成进度条上正在检测项名称
        var name = "#div_jian" + number.toString();
        var name1 = "#div_jian" + (number+1).toString();
        $(name).hide();
        $(name1).show();
        number++;
        }
   
}

//开始调用时钟函数和进度条
function start() {
//    //初始化第一个评价项图片
//    var div_value1 = document.getElementById("0EVAL_VALUE");
//    div_value1.innerHTML = '<div id="gybz">' + '<img src="images\\100.gif">' + '</div>';
//    //初始化总分图片
//    var div_zongfenValue = document.getElementById("img_zongfen");
//    div_zongfenValue.innerHTML = '<div id="img_zongfen">' + '<img src="images\\100zong.gif">' + '</div>';
    //生成进度条上正在检测项名称
    $("#div_zongfen").hide();
//    $("#div_jian0").show();
    number = 0;
    iID = setInterval(initioalValueDesc, 1200);
    $('#progress1').anim_progressbar(); //进度条启动
}
//*****************智能诊断部分结束*********************//
//*****************描图部分*********************//
///描一条曲线
function drawSingleSpline(ID) {
    $.ajax({
        url: 'ajax_sgtzd.ashx',
        type: 'get',
        dataType: 'xml', //这里可以不写，但千万别写text或者html!!! 
        data: { id: ID, field: "SGT" },
        timeout: 2000,
        cache: false,
        error: function (xml) {
            alert('数据有错误' + xml);
        },
        success: function (xml) {
            var data1 = [];
            $(xml).find("tables").children().each(function () {
               var l = $(this).children("l").text();
               var xdzh = $(this).children("XDZH").text();
               data1.push([Number(l), Number(xdzh)]);
           });
           swicthDrawCan(data1); //描典型功图
            var chart = $('#container').highcharts();
            chart.addSeries({
                data: data1,
                   name:'实际功图',
                   color: '#0000AA'
                });
               //            p.attr("title", chart.series.length - 1); 
           }
    });
   }
   ///取消一条曲线
   function cancelSingleSpline() {
       var chart = $('#container').highcharts();
       if (chart.series.length > 0) {
           chart.series[0].remove();
       }

   }
   ///清空描图区域
   function canselSpline() {
       var chart = $('#container').highcharts();
       if (chart.series.length) {//清空描图区域
           var len = chart.series.length;
           for (var i = 0; i < len; i++) {
               chart.series[0].remove();
           
           }
       }

   }
   ///求一组数据中的最大值
   function getValueMax(data) {
       var max = data[0];
       for (i = 1; i < data.length; i++) {
           if (data[i] >= max) max = data[i];
       }
       return max;
   }
   ///求一组数据中的最小值
   function getValueMin(data) {
       var min = data[0];
       for (i = 1; i < data.length; i++) {
           if (data[i] <= min) min= data[i];
       }
       return min;
   }
   //提取边界值和斜率
   function getBoundary(data) {
       var boundData = [];
       var Xarray = [];//驴头位移
       var Yarray = []; //悬点载荷
       for (var i = 0; i < data.length; i++) {
           Xarray.push(data[i][0]);
           Yarray.push(data[i][1]);
       }
       //起终点
       var X0 = Xarray[0];
       var Y0 = Yarray[0];
       var value = Yarray[10];
       var i = 11;
       //找到横坐标大于0.2的第一个点对应的下标号
       while (Xarray[i] < 0.2) {
           i++;
       }
       //找到一直增大的最大点
       while (Yarray[i] > value) {
           value = Yarray[i];
           i++;
       }
       var Y1 = Yarray[i-1];
       var X1 = Xarray[i - 1];

       var X2 = getValueMax(Xarray);
       var Y2 = Y1;
       var X3 = X2 - X1;
       var Y3 = Y0;
       boundData.push([X0,Y0]);
       boundData.push([X1, Y1]); 
       boundData.push([X2, Y2]); 
       boundData.push([X3, Y3]);
       boundData.push([X0, Y0]);
       //将初步理想功图放缩到和实际数据最大范围
       var Xdata = [boundData[0][0], boundData[1][0], boundData[2][0], boundData[3][0], boundData[4][0]];  //横坐标数组
       var Ydata = [boundData[0][1], boundData[1][1], boundData[2][1], boundData[3][1], boundData[4][1]]; //纵坐标数组
       var Xmin = getValueMin(Xdata);
       var Xmax = getValueMax(Xdata);
       var Ymin = getValueMin(Ydata);
       var Ymax = getValueMax(Ydata);
       var tmpXArr = new Array(Xdata.length);
       var tmpYArr = new Array(Xdata.length);
       var xScale = (getValueMax(Xarray) - boundData[0][0]) / (Xmax - Xmin);
       var yScale = (getValueMax(Yarray) - boundData[0][1]) / (Ymax - Ymin);
       for (var i = 0; i < Xdata.length; i++) {
           tmpXArr[i] = (Xdata[i] - Xmin) * xScale + boundData[0][0];
           tmpYArr[i] = (Ydata[i] - Ymin) * yScale + boundData[0][1];
       }
       data = getArray1(tmpXArr, tmpYArr);
       data.push([getValueMin(Yarray),getValueMax(Yarray)]);//存储载荷最小值和最大值
       return data;
   }
   //计算功图特征值
   function getValue(bounddata) {
//       var Xarray = [];//驴头位移
//       var Yarray = []; //悬点载荷
//       for (var i = 0; i < data.length; i++) {
//           Xarray.push(data[i][0]);
//           Yarray.push(data[i][1]);
//       }
       var GGCC = bounddata[2][0] - bounddata[0][0]; //光杆冲程
       var BCC = bounddata[3][0] - bounddata[0][0]; //泵冲程
       var XDZHMAX=bounddata[5][1];//载荷最大值
       var XDZHMIN=bounddata[5][0];; //载荷最小值
//       var ZGL; //做功量
//       XDZHMAX = getValueMax(Yarray);
//       XDZHMIN = getValueMin(Yarray);
//       var upzgl=0;//上冲程做功量
//       var downzgl = 0; //下冲程做功量
//       var i = 0;//下标
//       for (i = 0; i < Xarray.length / 2; i++) {
//           upzgl = upzgl + (Yarray[i + 1] + Yarray[i]) * (Xarray[i + 1] - Xarray[i]) / 2;
//       }
//       for (i = Xarray.length / 2; i < Xarray.length-1; i++) {
//           downzgl = downzgl + (Yarray[i + 1] + Yarray[i]) * (Xarray[i] - Xarray[i+1]) / 2;
//       }
//       upzgl = Math.round((upzgl + 0.001) * Math.pow(10, 2)) / Math.pow(10, 2);
//       downzgl = Math.round((downzgl + 0.001) * Math.pow(10, 2)) / Math.pow(10, 2);
//       ZGL = upzgl - downzgl;
//       ZGL = Math.round((ZGL + 0.001) * Math.pow(10, 2)) / Math.pow(10, 2);
//       //光杆冲程和泵冲程
//       GGCC = getValueMax(Xarray) - Xarray[0]; //光杆冲程
//       //找到一直增大的最大点
//       var value = Yarray[10];
//       var i = 11;
//       var ss = "";
//       while (i < Yarray.length) {
//           value = Yarray[i];
//           var f = (Yarray[i] - Yarray[i - 1]) / (Xarray[i] - Xarray[i - 1]);
//           if (f < 0.1 && Yarray[i] > Yarray[i + 1]) {
//               break;
//           }
//           i++;
//       }
//       var Y1 = Yarray[i];
//       var X1 = Xarray[i];
       $("#txt_xdzhmax").val(XDZHMAX);
       $("#txt_xdzhmin").val(XDZHMIN);
       GGCC = Math.round((GGCC + 0.001) * Math.pow(10, 2)) / Math.pow(10, 2);
       BCC = Math.round((BCC + 0.001) * Math.pow(10, 2)) / Math.pow(10, 2);
       $("#txt_ggcc").val(GGCC);
       $("#txt_bcc").val(BCC);
      
   }   
   ///描典型功图
   function swicthDrawCan(data) {
       switch (drawCan) {
           case "LLGT":
               {
                   drawLLGT();
                   var bounddata = getBoundary(data);
                   getValue(bounddata);
                   break;
               }
           case "LXGT":
               {
                   var bounddata = getBoundary(data);
                   drawLXGT(bounddata);
                   getValue(bounddata);
                   break;
               }
           case "GYBZ":
               { 
                   var bounddata = getBoundary(data);
                   drawGYBZ(bounddata);
                   getValue(bounddata);
                   break;
               }
           case "GDFELS":
               {
                   var bounddata = getBoundary(data);
                   drawGDFELS(bounddata);
                   getValue(bounddata);
                   break;
               }
           case "YDFELS":
               {
                   var bounddata = getBoundary(data);
                   drawGYBZ(bounddata);
                   getValue(bounddata);
                   break;
               }
           case "SFELS":
               {
                   var bounddata = getBoundary(data);
                   drawGYBZ(bounddata);
                   getValue(bounddata);
                   break;
               }
           case "YGLS":
               {
                   var bounddata = getBoundary(data);
                   drawGYBZ(bounddata);
                   getValue(bounddata);
                   break;
               }
           case "YJJL":
               {
                   var bounddata = getBoundary(data);
                   drawGYBZ(bounddata);
                   getValue(bounddata);
                   break;
               }
           case "YJCS":
               {
                   var bounddata = getBoundary(data);
                   drawGYBZ(bounddata);
                   getValue(bounddata);
                   break;
               }
           case "LCDP":
               {
                   var bounddata = getBoundary(data);
                   drawGYBZ(bounddata);
                   getValue(bounddata);
                   break;
               }
           case "CYGDT":
               {
                   var bounddata = getBoundary(data);
                   drawGYBZ(bounddata);
                   getValue(bounddata);
                   break;
               }
          
       }
   }
   //理论功图
   function drawLLGT() {
       var data = [];
       var q1 = 20.09; //每米抽油杆在液体中的质量；单位：牛顿/米
       var q2 = 9.506; //活塞以上每米液柱重量；单位：牛顿/米
       var L = 800; //抽油杆长度；单位：米
       var H = 0; //沉没度；单位：米
       var E = 2.1 * Math.pow(10, 11); //抽油杆弹性系数；2.1*10的11牛顿/米
       var BJ = 38; //泵径, 单位：毫米
       var S = 1.5; //光杆，单位：米
       var p1 = 860; //原油密度，单位:千克/米三次方
       var f1 = 2.85; //抽油杆截面积；单位：平方厘米
       var f2 = 11.9; //油管截面积：单位：平方厘米
       var LB = 810; //以悬点载荷为纵坐标，每毫米纵坐标表示的载荷称为力比，单位：牛顿/毫米
       var JCB = 30; //以悬点位移为横坐标，图上冲程与实际冲程之比值称为减程比
       //中间量计算
       var P1 = q1 * L; //抽油杆柱在液体中的重量
       var P2 = q2 * L; //活塞以上液柱的重量
       var OA = P1 / LB; //下冲程高度在图像上的长度
       var OB1 = (P1 + P2) / LB; //上冲程高度在图像上的长度
       var AD1 = S * 1000 / JCB; //光杆冲程在图像上的长度
       var CCSS = ((P2 * L) / (E *Math.pow(10,-4))* (1 / f1 + 1 / f2))*1000; //冲程损失
       var BB1 = CCSS / JCB; //冲程损失在图像上的长度
       data.push([0, OA]);
       data.push([BB1/10, OB1]);
       data.push([AD1 / 10, OB1]);
       data.push([AD1/10 - BB1/10, OA]);
       data.push([0, OA]);
       var chart = $('#container').highcharts();
       chart.addSeries({
           data: data,
           name: '理论功图',
           color: '#77FF00'
       });
   }
   //理想功图
   function drawLXGT(boundata) {
       var data = [];
       data.push([boundata[0][0], boundata[0][1]]); //0
       data.push([boundata[1][0], boundata[1][1]]); //1
       data.push([boundata[2][0], boundata[2][1]]); //2
       data.push([boundata[3][0], boundata[3][1]]); //3
       data.push([boundata[0][0], boundata[0][1]]); //0
       var chart = $('#container').highcharts();
       chart.addSeries({
           data: data,
           name:'理想功图',
           color: '#77FF00'
       });
   }
   //供液不足
   function drawGYBZ(boundata) {
       var data = [];
//       data.push([boundata[0][0], boundata[0][1]]);//0
//       data.push([boundata[1][0], boundata[1][1]]);//1
//       data.push([boundata[2][0], boundata[2][1]]);//2
//       data.push([boundata[2][0], boundata[2][1] - (1 / 3) * (boundata[2][1] - boundata[0][1])]); //3
//       data.push([boundata[3][0], boundata[2][1] - (1 / 3) * (boundata[2][1] - boundata[0][1])]); //4
//       var t = boundata[5][0] //斜率
//       data.push([boundata[3][0] - ((boundata[2][1] - (1 / 3) * (boundata[2][1] - boundata[0][1])) / t), boundata[0][1]]); //5
       //              data.push([boundata[0][0], boundata[0][1]]);//0


       var Xdata = [0.01, 0.01, 0.01, 0.01, 0.01, 0.04, 0.05, 0.07, 0.08, 0.1, 0.14, 0.17, 0.19, 0.22, 0.32, 0.37, 0.43, 0.51, 0.57, 0.71, 0.8, 0.87, 0.96, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4, 5.39, 5.37, 5.34, 5.28, 5.24, 5.21, 5.16, 3.88, 2.76, 0.01];  //横坐标数组
       var Ydata = [52.6, 52.6, 53.5, 55.1, 56, 57.1, 57.4, 57.9, 58.8, 59.2, 60.8, 61.7, 62.8, 64, 67, 68.5, 70.6, 72.7, 74.7, 78.6, 81.3, 83.4, 84.2, 84.2, 80.3, 80.7, 81, 81.2, 80.8, 80.7, 79.8, 77.4, 76.4, 76.2, 75.3, 74.2, 71.8, 71.4, 71.5, 52.6, 52.6]; //纵坐标数组
       var Xmin = getValueMin(Xdata);
       var Xmax = getValueMax(Xdata);
       var Ymin = getValueMin(Ydata);
       var Ymax = getValueMax(Ydata);
       var tmpXArr = new Array(Xdata.length);
       var tmpYArr = new Array(Xdata.length);
       var xScale = (boundata[2][0] - boundata[0][0]) / (Xmax - Xmin);
       var yScale = (boundata[2][1] - boundata[0][1]) / (Ymax - Ymin);

       for (var i = 0; i < Xdata.length; i++) {

           tmpXArr[i] = (Xdata[i] - Xmin) * xScale + boundata[0][0];
           tmpYArr[i] = (Ydata[i] - Ymin) * yScale + boundata[0][1];
       }

       data = getArray1(tmpXArr, tmpYArr);
       var chart = $('#container').highcharts();
       chart.addSeries({
           data: data,
           name: '典型功图',   
           color: '#77FF00'
       });
   }
   //固定凡尔漏失
   function drawGDFELS(boundata) {
       var data = [];
       var d = (1 / 2) * (boundata[2][0] - boundata[1][0]) + boundata[1][0]; //对称轴
       var k = boundata[2][1] / ((boundata[2][0] - d) * (boundata[2][0] - d)); //系数
       var h = (9/10) * boundata[0][1];//向上平移高度
       //第一段
       data.push([boundata[1][0], boundata[1][1]]); //0
       data.push([boundata[2][0], boundata[2][1]]); //1
       //第二段
       var X2 = d + Math.sqrt((1 / k) * (boundata[0][1] - h));
       data.push([X2, k * (X2 - d) * (X2 - d) + h]); //2
//       for (var x = boundata[2][0]; x > X2; x = x - 0.01) {
//           data.push([x, k*(x - d) * (x - d)+h]);//2
       //       }

       //第三段
       var X3 = d - Math.sqrt((1 / k) * (boundata[0][1] - h));
       data.push([X3, boundata[0][1]]); //3
       //第四段
       data.push([boundata[1][0], boundata[1][1]]); //0
       var chart = $('#container').highcharts();
       chart.addSeries({
           data: data,
           name: '典型功图',
           color: '#77FF00'
       });
   }
  //*****************描图部分结束*********************//
var fun;
$(document).ready(fun = function () {
    //开始诊断
    $('#btn_start').click(function () {
        start();
    });
    $('#tt').datagrid({ onClickRow: function (rowIndex) {//代码 
        var row = $('#tt').datagrid('getSelected');
        if (row == null)
            return;
        var ID = row.ID; //对应的记录ID
        canselSpline();
        currentID = ID;
        drawSingleSpline(ID); //描出对应曲线


    }
});
     //返回主页面
    $('#backMain').click(function () {
        parent.openSonPage("analysisMain.aspx");
    });

    //点击理论功图
    $("#btn_LLGT").click(function () {//理论功图
        drawCan = "LLGT";
        canselSpline();
        drawSingleSpline(currentID);
    });
    //点击理想功图
    $("#btn_LXGT").click(function () {//理论功图
        drawCan = "LXGT";
        canselSpline();
        drawSingleSpline(currentID);
    });
    //点击供液不足
    $("#btn_GYBZ").click(function () {//供液不足
        drawCan = "GYBZ";
        canselSpline();
        drawSingleSpline(currentID);
    });
    //点击固定凡尔漏失
    $("#btn_GDFELS").click(function () {//供液不足
        drawCan = "GDFELS";
        canselSpline();
        drawSingleSpline(currentID);
    });
    
    ///描图控件属性设置
    $('#container').highcharts({
        colors: [
                 '#ff7e08',
                 '#fd230a',
                 '#fbbc01',
                 '#9ff0000',
                 '#1afd0e',
                 '#4929f0',
                 '#f28ff3',
                 '#77a1f5',
                 '#c4f505',
                 '#f6c90a'
             ],

        chart: {
            zoomType: 'x',
            type: 'spline',
            marginRight: 5,
            events: {
                load: function () {

                }
            }
        },
        xAxis: {
            max: null, //360,
            tickInterval: null
        },

        title: {
            text: '',
            x: -20 //center
        },
        subtitle: {
            text: '',
            x: -20
        },

        yAxis: {
            title: {
                text: ''
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            valueSuffix: ''
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle',
            borderWidth: 0
        },
        plotOptions: {

            series: {
                animation: {
                    duration: 10
                }
            },

            spline: {
                lineWidth: 2,
                states: {
                    hover: {
                        lineWidth: 3
                    }
                },
                marker: {
                    enabled: false
                }

            }

        },
        series: []
    }); //属性设置结束


});